//
//  SECitiesController.m
//  kuaiyi
//
//  Created by Alphts on 16/7/6.
//  Copyright © 2016年 Alphts. All rights reserved.
//

#import "SECitiesController.h"
#import "SEWeatherDTO.h"
@interface SECitiesController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation SECitiesController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cityCell"];
    
}

//重写城市数组的set方法
- (void)setCities:(NSArray *)cities{
    _cities = cities;
    
    [self.tableView reloadData];
    
}

#pragma mark - 数据源和代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.cities.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cityCell" forIndexPath:indexPath];
    
    cell.textLabel.text = self.cities[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //判断是否联网 没有的话直接弹框返回
    
    [SENetworkReachabilityTool checkNetworkWithReachableBlock:^{
        
        //访问数据 用block将数据返回给homeController
        [SEWeatherDTO modelWithCityName:self.cities[indexPath.row] successBlock:^(SEWeatherDTO *model) {
            
            if (self.cityBlock) {
                self.cityBlock(model,self.cities[indexPath.row]);
            }
            
        } errorBlock:^{
            NSLog(@"error");
        }];
        
        [self.navigationController popToRootViewControllerAnimated:YES];
        
    } unReachableBlock:^{
        
        [SVProgressHUD setMinimumDismissTimeInterval:1];
        
        [SVProgressHUD showErrorWithStatus:@"当前网络不佳,请检查网络"];
        
    }];

    [self.navigationController popToRootViewControllerAnimated:YES];
}



@end









